package com.run.runbbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.run.runbbs.common.dto.ListVO;
import com.run.runbbs.common.dto.PageContent;
import com.run.runbbs.common.dto.UserInfoDto;
import com.run.runbbs.common.lang.Result;
import com.run.runbbs.entity.UserInfo;
import com.run.runbbs.service.UserInfoService;
import com.run.runbbs.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private JwtUtils jwtUtils;

    @RequiresAuthentication
    @GetMapping("/get")
    public Result get(HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        UserInfo userInfo=userInfoService.getOne(new QueryWrapper<UserInfo>().eq("user_id",userId));
        return Result.success(userInfo);
    }

    @PostMapping("/add")
    public Result add(@RequestBody UserInfoDto userInfoDto, HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        userInfoDto.setUserId(userId);
        UserInfo userInfo=new UserInfo();
        BeanUtils.copyProperties(userInfoDto,userInfo);
        userInfo.setCreateTime(new Date());
        userInfoService.save(userInfo);
        return Result.success("添加成功");
    }
    @PutMapping("/edit")
    public Result edit(@RequestBody UserInfoDto userInfoDto){
        UserInfo userInfo=new UserInfo();
        BeanUtils.copyProperties(userInfoDto,userInfo);
        try{
            if(userInfoService.getById(userInfo.getId())==null){
               Result.fail("未找到对应实体");
            }else{
                userInfoService.updateById(userInfo);
                Result.success("修改成功");
            }

        }catch (Exception e){
            log.error(e.getMessage(),e);
            Result.fail("修改失败"+e.getMessage());
        }
        return Result.success("修改成功");
    }
    @PostMapping("/list")
    public Result list(@RequestBody @NotNull ListVO listVO, HttpServletRequest request) {
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        Page<UserInfo> page = new Page<>(listVO.getPage(), listVO.getPageSize());
//        log.info("userid为 {} ", userId);
        UserInfo userInfo = userInfoService.getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserId, userId).select(UserInfo::getAddress));
        IPage<UserInfo> iPage = userInfoService.page(page, new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getAddress, userInfo.getAddress()));

        List<UserInfo> list = iPage.getRecords();


        return Result.success(
                PageContent.<UserInfo>builder()
                        .columns(null)
                        .list(list)
                        .total(iPage.getTotal()).build());
    }

}
